import os,sys
sys.path.append(os.path.dirname(sys.path[0]))
from fastapi import APIRouter,status,HTTPException,Request
from db.db_operate import kinetica
from models.collections import create_collections
import json

collections_api = APIRouter()


@collections_api.get("/collections")
def get_collections(request:Request):
    
    try:
        host = request.url.hostname
        port = request.url.port
        response=kinetica.get_collections()
        ccs=create_collections(response,host,port).create_coll()
        
        return ccs
    except:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)

    
@collections_api.get("/collections/{collection_id}")
def get_collectionid(collection_id,request:Request):
    
    host = request.url.hostname
    port = request.url.port
   
  
    ccs=create_collections(collection_id,host,port).create_collid()

    return ccs
        
@collections_api.get("/collections/{collection_id}/counts")
def get_collectionid(collection_id,request:Request):
    
   
    response=kinetica.get_collection_counts(collection_id)

    counts=json.loads(response["json_encoded_response"])["column_1"][0]
    
    return {"collection":f"{collection_id}","counts":counts}
        





